﻿/**********************************************************\
*  TinyLib - Camera                                        *
*                                                          *
*                                                          *
* Last edited: Jens Berglind  (10/07/2013)                 *
\**********************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

using TinyLib.Input;

namespace TinyLib.Camera
{
    public enum CameraMode
    {
        Free,
        Center,
        ZoomIn,
        ZoomOut
    }

    public enum PlayerDistance
    {
        Distance1 = 500,
        Distance2 = 1000,
        Distance3 = 1500,
        Distance4 = 2000
    };

    public class Camera : GameComponent
    {
        #region Fields

        protected float fZoom;
        public Matrix transform;
        public Vector2 CamPosition;
        public Vector2 CamPosition2;
        protected float fRotation;
        public float fMaxZoom;
        public float fMinZoom;
        private IniFile config;
        Keys camUp, camDown, camRight, camLeft;
        public Viewport viewport;
        public Vector2 camDirection = new Vector2(0.0f, 0.0f);

        #endregion

        #region Properties

        public float FloatZoom
        {
            get { return fZoom; }
            set
            {
                fZoom = value;

                if (fZoom < fMinZoom)
                {
                    fZoom = fMinZoom;
                }

                else if (fZoom > fMaxZoom)
                {
                    fZoom = fMaxZoom;
                }
            }
        }

        public float FloatRotation
        {
            get { return fRotation; }
            set { fRotation = value; }
        }

        public Vector2 CameraPosition
        {
            get { return CamPosition; }
            set { CamPosition = value; }
        }

        #endregion

        #region Inits

        public Camera(Game game, Viewport viewport)
            : base(game)
        {
            config = new IniFile("config.ini");
            config.Parse();

            //fMaxZoom = float.Parse(config.GetValue("Camera", "MaxZoom"));
            //fMinZoom = float.Parse(config.GetValue("Camera", "MinZoom"));

            this.viewport = viewport;

            fZoom = 1.0f;
            fRotation = 0.0f;
            CamPosition = new Vector2(
                                        viewport.Width * 0.5f,
                                        viewport.Height * 0.5f
                                      );
            CamPosition2 = new Vector2(
                                        viewport.Width * 0.5f,
                                        viewport.Height * 0.5f
                                      );

            // viewport = new Viewport(0, 0, 
        }

        #endregion

        #region Methods

        public void Update(GameTime gameTime)
        {
            KeyboardState keyState = Keyboard.GetState();
            float fDeltaTime = (float)gameTime.ElapsedGameTime.Milliseconds;
        }

        public void Move(Vector2 newCamPos)
        {
            CamPosition += newCamPos;
        }


        public Matrix get_transformation(GraphicsDevice graphicsDevice)
        {
            transform = Matrix.CreateTranslation
                        (
                            new Vector3(-CamPosition.X, -CamPosition.Y, 0)) *
                            Matrix.CreateRotationZ(FloatRotation) *
                            Matrix.CreateScale(new Vector3(FloatZoom, FloatZoom, 1)) *
                            Matrix.CreateTranslation(new Vector3(viewport.Width * 0.5f,
                                                                 viewport.Height * 0.5f,
                                                                 0));

            return transform;
        }

        #endregion
    }
}